/**
 * Created by Administrator on 2017/7/4 0004.
 */
require(["jquery", "paging", "bootstrap", "bbcare", "jquery.dataTables", 'bootstrap.dataTables', "scprit/department/vm/assessmentScreening/assessmentScreeningQuestion.servers.js"], function ($, Paging, bootstrap, $bb, $jTable, $bTable, $servers) {
    var URL = $bb.baseUrl("department") + "followup/service"
    var obj = $bb.getPageParams()
    var userId = obj.userId
    var type = obj.type//历史记录传参；
    var count = 1, pagesize = 20
    var HashMap = $servers.hashmap(), hashMap = new HashMap()
    var historyTitle = $("#historyTitle")

    getBabyHistoryList(1, 20)
    var p = new Paging()
    p.init({
        target: '#pageTool', pagesize: pagesize, count: count, toolbar: false, callback: function (page, size, count) {
            getBabyHistoryList(page, size)
        }
    })

    if (type == "HighriskPregnant" || 'SAS' || 'SDS' || 'EPDS') {
        $("#dataList").on('click', "ul", function () {
            var answerId = $(this).find("a[aim='view']").attr("result")
            var data = hashMap.get(answerId)
            obj.answerId = answerId
            obj.answer = data
            if (type == "HighriskPregnant") {
                $bb.openPage("highRiskPregnantwomen", obj)
            }
            if (type == 'SAS') {
                $bb.openPage("anxiety", obj)
            }
            if (type == 'SDS') {
                $bb.openPage("depression", obj)
            }
            if (type == 'EPDS') {
                $bb.openPage("postpartumDepression", obj)
            }
        })
    }

    $("#dataList").on('click', "ul li a", function () {
        var answerId = $(this).attr("result")
        if ($(this).attr("aim") == "view") {
            var data = hashMap.get(answerId)
            obj.answerId = answerId
            obj.answer = data
            var testTime = $(this).parent("li").attr("testTime")
            obj.testTime = testTime
            obj.business = $(this).parent("li").attr("business")
            if (type == "HighriskPregnant") {
                $bb.openPage("highRiskPregnantwomen", obj)
            }
            if (type == 'SAS') {
                $bb.openPage("anxiety", obj)
            }
            if (type == 'SDS') {
                $bb.openPage("depression", obj)
            }
            if (type == 'EPDS') {
                $bb.openPage("postpartumDepression", obj)
            }
            if (type == 'AIMS') {
                $bb.openPage("albertaAssessmentOfInfants", obj)
            }
        } else if ($(this).attr("aim") == "viewReport") {
            if (type == 'AIMS') {
                obj.answerId = answerId
                $bb.openPage("albertaAssessmentOfInfantsResult", obj)
            }
        }
    })
    function getBabyHistoryList (page, rows) {
        var params = {
            "userId": userId,
            "examinationPaperName": type,
            "page": page,
            "rows": rows
        }
        if (type == "HighriskPregnant") {
            historyTitle.html("高危孕产妇历史评测记录")
        }
        if (type == 'SAS') {
            historyTitle.html("焦虑自评历史评测记录")
        }
        if (type == 'SDS') {
            historyTitle.html("自评抑郁历史评测记录")
        }
        if (type == 'EPDS') {
            historyTitle.html("爱丁堡产后抑郁历史评测记录")
        }
        if (type == 'AIMS') {
            $(".resShow").css("display", "none")
            historyTitle.html("Alberta婴幼儿运动历史评测记录")
        }
        $bb.ajax({
            url: URL + "/patient/evaluation/getHistoryScore",//baby地址
            async: false,
            data: JSON.stringify({
                data: params
            }),
            success: function (data) {
                if (data.success) {
                    hashMap = new HashMap()
                    count = data.total
                    var str = ''
                    for (var i = 0; i < data.rows.length; i++) {
                        var res = {
                            "answerResult": data.rows[i].answerResult,
                            "result": data.rows[i].result
                        }
                        hashMap.put(data.rows[i].answerId, res)
                        var actualDay = $servers.dateDiff(data.rows[i].birthday, data.rows[i].createTime.substring(0, 10))
                        var age = $servers.age(actualDay, data.rows[i].pregnancyWeek)
                        var typeQues = data.rows[i].type == "HighriskPregnant" ? "高危孕产妇评测" : data.rows[i].type == "EPDS" ? "爱丁堡产后抑郁评测" : data.rows[i].type == "SAS" ? "焦虑自评测" : data.rows[i].type == "SDS" ? "自评抑郁" : "Alberta婴幼儿运动量表"
                        var resultShow = "&nbsp;", className
                        if (type != "AIMS") {
                            resultShow = JSON.parse(data.rows[i].result).map.level
                            var resLevel = resultShow.substring(0, 2)
                            if (type != 'EPDS') {
                                className = resLevel == "轻度" ? 'light' : resLevel == "中度" ? "middle" : resLevel == "重度" ? "high" : "normal"
                            }
                        }
                        str += '<ul class="clearfix" >'
                        if (type != "AIMS") {
                            if (type == 'EPDS') {
                                resultShow = resultShow == "正常" ? "正常" : resultShow == "可诊断为产后抑郁" ? "产后抑郁" : "综合干预"
                                className = resultShow == "正常" ? "normal" : resultShow == "产后抑郁" ? "light" : "high"
                            }
                            str += '<li class="resShow ' + className + '">' + resultShow + '</li>'
                        } else {
                            str += '<li class="resShow" style="display:none">' + resultShow + '</li>'
                        }
                        str += '<li class="name">' + data.rows[i].babyName + '</li>'
                        str += '<li>' + age + '</li>'
                        str += '<li>' + data.rows[i].phoneNo + '</li>'
                        str += '<li class = "quesType">' + typeQues + '</li>'
                        str += '<li class = "quesTime">' + data.rows[i].createTime + '</li>'
                        if (type == "AIMS") {
                            str += '<li testTime = "' + data.rows[i].testTime + '"><a class="view" aim = "view" result="' + data.rows[i].answerId + '">评估问卷</a>&nbsp;&nbsp;<a aim = "viewReport" result="' + data.rows[i].answerId + '">评估报告</a></li>'
                        } else {
                            str += '<li><a classview aim = "view" result="' + data.rows[i].answerId + '">评估问卷</a></li>'
                        }
                        str += '</ul>'
                    }
                    $("#dataList").html(str)
                    if (window.__config__.role.indexOf('ReadOnly') > -1) {
                        $('.view').css({
                            display: 'none'
                        })

                    }
                }
            }
        })
    }
})